package com.example.asus.shixun2;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatButton;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.example.asus.shixun2.SQL.DBOpenHelper;
import com.example.asus.shixun2.control.GameControl;

import java.util.Random;

public class MainActivity extends AppCompatActivity{
    FrameLayout frameLayout;
    View ver_View;
    Paint rect_paint;
    Paint ovel_paint;
    Paint text_paint;
    Random random = new Random();
    AppCompatButton bt_next;
    AppCompatButton bt_login;
    AppCompatButton bt_regist;
    EditText et_user;
    EditText et_psw;
    EditText et_ver;
    String ver1;
    GameControl gameControl;
    String username;
    String password;
    String ver;



    private DBOpenHelper dbOpenHelper;//声明DBOpenHelper对象


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        setContentView(R.layout.activity_main);
        setTitle("俄罗斯方块");

        dbOpenHelper = new DBOpenHelper(MainActivity.this,"login",null,1){//实例化DBOpenHelper对象

        };
        initPaint();


        frameLayout = (FrameLayout)findViewById(R.id.ver_layout);
        ver_View = new View(this){
            @Override
            protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                canvas.drawRect(new RectF(0,0,180,60),rect_paint);
                for(int i = 0;i < 100;i ++){
                    int x = random.nextInt(175)+5;
                    int y = random.nextInt(55)+5;
                    int m = random.nextInt(2)+2;
                    canvas.drawCircle(x,y,m,ovel_paint);
                }
                String str = "1234567890qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM";
                char [] c1 = str.toCharArray();
                StringBuffer sb = new StringBuffer();
                for(int i = 0;i < 4;i ++){
                    int pos = random.nextInt(62);
                    sb.append(c1[pos]);
                }
                String str1 = sb.toString();
                ver1 = str1;
                canvas.drawText(str1,50,45,text_paint);
            }
        };
        ver_View.setBackgroundColor(Color.GRAY);
        ver_View.setLayoutParams(new ActionBar.LayoutParams(180,60));
        //添加到游戏区域父容器
        frameLayout.addView(ver_View);

        et_user = (EditText)findViewById(R.id.et_username);
        et_psw = (EditText)findViewById(R.id.et_password);
        et_ver = (EditText)findViewById(R.id.et_Ver);

        bt_next = (AppCompatButton)findViewById(R.id.bt_nextVer);
        bt_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ver_View.invalidate();
            }
        });

        bt_login = (AppCompatButton)findViewById(R.id.bt_login);
        bt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username = et_user.getText().toString();
                password = et_psw.getText().toString();
                ver = et_ver.getText().toString();
                if(username.equals("")){
                    String str = "用户名不能为空";
                    warning(str);
                    return;
                }else if(password.equals("")){
                    String str = "密码不能为空";
                    warning(str);
                    return;
                }else if(ver.equals("")){
                    String str = "验证码不能为空";
                    warning(str);
                    return;
                }else{
                    if(sureLogin(username,password)){
                        Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                        startActivity(intent);
                    }else{
                        String str = "密码错误";
                        warning(str);
                        return;
                    }

                }

            }
        });

        bt_regist = (AppCompatButton)findViewById(R.id.bt_regist);
        bt_regist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,REGIST.class);
                startActivity(intent);
            }
        });
    }




    public void warning(String str){
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    public boolean sureLogin(String username,String password){

        Cursor cursor = dbOpenHelper.getReadableDatabase().query ("login",null,null,null,null,null,null);

//判断游标是否为空
        if(cursor.moveToFirst()){
//遍历游标
            for (int i = 0; i < cursor.getCount(); i++) {
                cursor.move(i);
                String username1 = cursor.getString(0);
                String password1 = cursor.getString(1);
                if(username.equals(username1)){
                    if(password.equals(password1)){
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**初始化画笔*/
    private void initPaint() {
        rect_paint = new Paint();//初始化辅助线画笔
        rect_paint.setColor(Color.BLACK);
        rect_paint.setAntiAlias(true);//打开抗锯齿
        rect_paint.setStyle(Paint.Style.STROKE);
        rect_paint.setStrokeWidth(8);// 设置“空心”的外框的宽度

        ovel_paint = new Paint();//初始化辅助线画笔
        ovel_paint.setColor(Color.BLUE);
        ovel_paint.setAntiAlias(true);//打开抗锯齿

        text_paint = new Paint();//初始化辅助线画笔
        text_paint.setColor(Color.RED);
        text_paint.setTextSize(40);
        text_paint.setAntiAlias(true);//打开抗锯齿
    }
}
